﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoVanHocTapData.Interface;
using CoVanHocTapData.Entities;

namespace CoVanHocTapData.Concretes
{
    public class SinhVien_TichLuyRepositpry:ISinhVien_TichLuyRepository
    {
        #region property
        DangKyPhuEntities entities;
        #endregion

        #region repos
        IKetQuaHocRepository ketQuaHocRepos = new KetQuaHocRepository();
        #endregion

        #region constructor
        public SinhVien_TichLuyRepositpry()
        {
            entities = new DangKyPhuEntities();
        }
        #endregion

        public IQueryable<Entities.SinhVien_TichLuy> LaySinhVienTichLuy(string MSSV, string NganhID, string HeDaoTaoID,int HocKyID)
        {
            return from m in entities.SinhVien_TichLuy
                   where m.MSSV.Equals(MSSV)
                   && m.NganhID.Equals(NganhID)
                   && m.HeDaoTaoID.Equals(HeDaoTaoID)
                   && m.HocKyDeTinhID.Value==HocKyID
                   select m; 
        }

        public bool KiemTraSinhVienTichLuy(string MSSV, string NganhID, string HeDaoTaoID,int HocKyID)
        {
            var result = from m in entities.SinhVien_TichLuy
                         where m.MSSV.Equals(MSSV)
                         && m.NganhID.Equals(NganhID)
                         && m.HeDaoTaoID.Equals(HeDaoTaoID)
                         && m.HocKyDeTinhID.Value == HocKyID
                         select m;
            if (result == null || result.Count() == 0)
                return false;
            return true;
        }

        public void ThemSinhVienTichLuy(SinhVien sv, string NganhID, string HeDaoTaoID, int HocKyID)
        {
                        
            SinhVien_TichLuy svTichLuy = new SinhVien_TichLuy()
            {
                HeDaoTaoID = HeDaoTaoID,
                MSSV = sv.MSSV,
                NganhID = NganhID,
                TrungBinhTichLuy = ketQuaHocRepos.TinhDTBTichLuy(sv,NganhID,HeDaoTaoID),
                TinChiTichLuy = ketQuaHocRepos.TinhSoTCTichLuy(sv,NganhID,HeDaoTaoID),
                HocKyDeTinhID = HocKyID
            };
            entities.AddToSinhVien_TichLuy(svTichLuy);
            entities.SaveChanges();
            
            
        }

        public void XoaSinhVienTichLuy(string MSSV, string NganhID, string HeDaoTaoID,int HocKyID)
        {
            if (KiemTraSinhVienTichLuy(MSSV, NganhID, HeDaoTaoID, HocKyID))
            {
                XoaSinhVienTichLuy(MSSV, NganhID, HeDaoTaoID,HocKyID);
                entities.SaveChanges();
            }
        }


        public void ThemSinhVienTichLuy(SinhVien_TichLuy sv)
        {
            entities.AddToSinhVien_TichLuy(sv);
            entities.SaveChanges();
        }


        public void UpdateSinhVienTichLuy(SinhVien_TichLuy sv)
        {
            var result = (from m in entities.SinhVien_TichLuy
                         where m.MSSV.Equals(sv.MSSV)
                         && m.NganhID.Equals(sv.NganhID)
                         && m.HeDaoTaoID.Equals(sv.HeDaoTaoID)
                         select m).FirstOrDefault();
            result.TrungBinhTichLuy = sv.TrungBinhTichLuy;
            result.TinChiTichLuy = sv.TinChiTichLuy;
            result.HocKyDeTinhID = sv.HocKyDeTinhID;
            result.TongTinChiNganh = sv.TongTinChiNganh;
            entities.SaveChanges();
        }

        #region ISinhVien_TichLuyRepository Members


        public bool LaSinhVienYeuKem(SinhVien sv,int HocKyID)
        {
            ThangDiemDanhGiaRepository thangDiemRepos = new ThangDiemDanhGiaRepository();
            
            if (!KiemTraSinhVienTichLuy(sv.MSSV, sv.Lop.NganhID,sv.Lop.HeDaoTaoID,HocKyID))
            {
                ThemSinhVienTichLuy(sv, sv.Lop.NganhID, sv.Lop.HeDaoTaoID, HocKyID);
            }
            var sv_TichLuy = LaySinhVienTichLuy(sv.MSSV, sv.Lop.NganhID, sv.Lop.HeDaoTaoID, HocKyID).FirstOrDefault();
            var thangDiemYeu = thangDiemRepos.LayThangDiemDanhGia("yếu").FirstOrDefault();
            var thangDiemKem = thangDiemRepos.LayThangDiemDanhGia("kém").FirstOrDefault();
            if ((sv_TichLuy.TrungBinhTichLuy.Value >= thangDiemYeu.TuGiaTri.Value && sv_TichLuy.TrungBinhTichLuy.Value <= thangDiemYeu.DenGiaTri.Value) || (sv_TichLuy.TrungBinhTichLuy.Value >= thangDiemKem.TuGiaTri.Value && sv_TichLuy.TrungBinhTichLuy.Value <= thangDiemKem.DenGiaTri.Value))
            {
                return true;
            }
            return false;
        }

        #endregion
    }
}
